<?php
class BolsasModel extends Zwei_Db_TableLoggeable
{
	protected $_name = "bolsas";
	protected $_primary = "id_bolsa";
	protected $_adapter = "an";
	
	public function select() 
	{
		$this->setAdapter("dn");
		$select = new Zend_Db_Table_Select($this);
		$select->order('id_bolsa');
		return $select;
	}
	
	protected function cleanDataParams($data)
	{
		if (isset($data['visible'])) 
		    $data['visible'] = $data['visible'] == 'S' ? 'S' : 'N';
		if (isset($data['U2C'])) 
		    $data['U2C'] = $data['U2C'] == 'true' ? 'true' : 'false';
		if (isset($data['LDI'])) 
		    $data['LDI'] = $data['LDI'] == 'true' ? 'true' : 'false';
		if (isset($data['Roaming'])) 
		    $data['Roaming'] = $data['Roaming'] == 'true' ? 'true' : 'false';
		return $data;
	}
	
	public function update($data, $where) 
	{
	    Debug::write("an");
		$this->setAdapter("an");
		$data = self::cleanDataParams($data);
		try {
            $update = parent::update($data, $where);
		} catch (Zend_Db_Exception $e) {
		  	if ($e->getCode() == 23000) {
		  		$this->setMessage("ID de bolsa ya está en uso");
		  	} else {
		  		Debug::write("Error code:" . $e->getCode() . ", mensaje:" . $e->getMessage());
		  	}
		  	return false;
		}
        return $update;
		
	}
	 
	public function insert($data)
	{
		$this->setAdapter("an");
		$data = self::cleanDataParams($data);
		try {
	        $insert = parent::insert($data);	
		} catch (Zend_Db_Exception $e) {
            if ($e->getCode() == 23000) {
                $this->setMessage("ID de bolsa ya está en uso");
            } else {
                Debug::write("Error code:" . $e->getCode() . ", mensaje:" . $e->getMessage());
            }
            return false;
        }
        return $insert;	       
	}
	
	public function selectSms() 
	{
        $this->setAdapter("dn");		
		$select = new Zend_Db_Table_Select($this);
		$select->where('unidad = ?', 'SMS_TD');
		$select->orWhere('unidad = ?', 'SMS_ONNET');
		
		return $select;
	}
	
	
    public function selectMms() 
    {
        $this->setAdapter("dn");    	
        $select = new Zend_Db_Table_Select($this);
        $select->where('unidad = ?', 'MMS_TD');
        $select->orWhere('unidad = ?', 'MMS_ONNET');
        
        return $select;
    }

    public function selectMin() 
    {
        $this->setAdapter("dn");    	
        $select = new Zend_Db_Table_Select($this);
        $select->where('unidad = ?', 'MIN');

        return $select;
    }    

    public function selectGprs() 
    {
        $this->setAdapter("dn");    	
        $select = new Zend_Db_Table_Select($this);
        $select->where('unidad = ?', 'KB');
        $select->orwhere('unidad = ?', 'BAM_KB');
        $select->orwhere('unidad = ?', 'BAM_HH');
        $select->orwhere('unidad = ?', 'NEM_KB');
        $select->orwhere('unidad = ?', 'NEM_HH');                        
        
        return $select;
    }

    public function selectSpinvox() 
    {
        $this->setAdapter("dn");
    	$select = new Zend_Db_Table_Select($this);
        $select->where('unidad = ?', 'SPINVOX');
        $select->orwhere('unidad = ?', 'SPINVOXNOR');        
        
        return $select;
    }

    public function selectMovitalk() 
    {
        $this->setAdapter("dn");
        $select = new Zend_Db_Table_Select($this);
        $select->where('unidad = ?', 'MOVITALK');
        
        return $select;
    }

    public function selectNfmins()
    {
    	$this->setAdapter("dn");
        $select = new Zend_Db_Table_Select($this);
        $select->where('unidad = ?', 'NFMINS');
        
        return $select;
    }

    public function selectNfdias()
    {
    	$this->setAdapter("dn");
        $select = new Zend_Db_Table_Select($this);
        $select->where('unidad = ?', 'NFDIAS');
        
        return $select;
    }    
	
}